Imports DiceShenanigensUserControls

Public Class gridBox

   Private playerNum As New PlayerNumber
   Private playerColor As Color
   Private OrgPlayerColor As Color
   Private colorChange As Integer = 90
   Private parentFrm As GridFormInterface
   Private dice As Integer
   Private red As Integer
   Private green As Integer
   Private blue As Integer
   Private _mode As gridMode

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="num"></param>
   ''' <remarks></remarks>
   Event boxClicked(ByVal num As Integer)

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public ReadOnly Property index() As Integer
      Get
         Return Convert.ToInt32(Me.Name)
      End Get
   End Property

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property gridMode() As gridMode
      Get
         Return _mode
      End Get
      Set(ByVal value As gridMode)
         _mode = value
      End Set
   End Property

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property leftBorder() As Boolean
      Get
         If (borderLeft.BackColor = Color.Maroon) Then
            Return True
         Else
            Return False
         End If
      End Get
      Set(ByVal value As Boolean)
         If value = True Then
            borderLeft.BackColor = Color.Orange
         Else
            borderLeft.BackColor = playColor
         End If
      End Set
   End Property

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property topBorder() As Boolean
      Get
         If (borderUp.BackColor = Color.Maroon) Then
            Return True
         Else
            Return False
         End If
      End Get
      Set(ByVal value As Boolean)
         If value = True Then
            borderUp.BackColor = Color.Orange
         Else
            borderUp.BackColor = playColor
         End If
      End Set
   End Property

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property numDice() As String
      Get
         Return dice
      End Get
      Set(ByVal value As String)
         dice = value
         txtvalue.Text = dice
      End Set
   End Property

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property player() As PlayerNumber
      Get
         Return playerNum
      End Get
      Set(ByVal value As PlayerNumber)
         If value <> playerNum Then
            OrgPlayerColor = playColor
         End If
         playerNum = value
      End Set
   End Property

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property playColor() As Color
      Get
         Return playerColor
      End Get
      Set(ByVal value As Color)
         playerColor = value
         txtvalue.BackColor = playerColor
         red = playerColor.R
         green = playerColor.G
         blue = playerColor.B
      End Set
   End Property

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="num"></param>
   ''' <param name="playColor"></param>
   ''' <param name="playNum"></param>
   ''' <param name="parentFrm"></param>
   ''' <param name="mode"></param>
   ''' <remarks></remarks>
   Public Sub New(ByVal num As Integer, ByVal playColor As Color, ByVal playNum As Integer, ByVal parentFrm As GridFormInterface, ByVal mode As gridMode)

      ' This call is required by the Windows Form Designer.
      InitializeComponent()

      _mode = mode

      ' Add any initialization after the InitializeComponent() call.
      If _mode = gridMode.Playing Then
         dice = num
         txtvalue.Text = num
      End If

      Me.parentFrm = parentFrm

      player = playNum

      playerColor = playColor
      OrgPlayerColor = playerColor
      txtvalue.BackColor = playColor

      borderLeft.BackColor = Color.Gray
      borderUp.BackColor = Color.Gray

      red = playerColor.R
      green = playerColor.G
      blue = playerColor.B

   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <remarks></remarks>
   Public Sub colorUp()

      playColor = OrgPlayerColor

   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <remarks></remarks>
   Public Sub colorDown()

      If red > colorChange Then
         red -= colorChange
      End If

      If green > colorChange Then
         green -= colorChange
      End If

      If blue > colorChange Then
         blue -= colorChange
      End If

      playColor = Color.FromArgb(red, green, blue)

   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub gridBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click, txtvalue.Click

      If _mode = gridMode.Creating Then

         RaiseEvent boxClicked(Me.Name)

      ElseIf _mode = gridMode.Playing Then

         If parentFrm.reinforceStage Then

            If playerTurn = playerNum And parentFrm.numReinforce > 0 And numDice < 10 Then

               numDice = numDice + 1
               parentFrm.numReinforce = parentFrm.numReinforce - 1

            Else

               Beep()

            End If

         Else

            If clicked And selectedSquare = Me.Name Then

               selectedSquare = -1
               clicked = False
               colorUp()

            ElseIf clicked And selectedSquare <> -1 And playerTurn <> playerNum And nextTo() Then

               parentFrm.battle(selectedSquare, Me.Name)
               clicked = False

            ElseIf Not clicked And playerTurn = playerNum And numDice > 1 Then

               clicked = True
               selectedSquare = Me.Name
               colorDown()

            Else

               Beep()

            End If

         End If

      End If

   End Sub

   ''' <summary>
   ''' 
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Private ReadOnly Property nextTo() As Boolean
      Get

         If Me.Name - 1 = selectedSquare Or Me.Name + 1 = selectedSquare Or _
               Me.Name + parentFrm.gridHeight = selectedSquare Or _
               Me.Name - parentFrm.gridHeight = selectedSquare Then
            Return True
         Else
            Return False
         End If

      End Get
   End Property

End Class
